﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using Skynet.BusinessEntities;
using System.Runtime.Serialization;
using Skynet.BusinessLogic.BusinessComponents;

namespace Skynet.WebAdministracion.Views.CategoriesMgm
{
    [System.ComponentModel.DataObject]
    public class CategoriesMgmAzurePresenter : Infrastructure.Web.Views.Master.MasterPresenter<ICategoriesMgmView>
    {

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }


        public override void OnViewInitialized()
        {
            base.OnViewInitialized();
        }
        public override void OnViewLoaded()
        {
            base.OnViewLoaded();
        }

        private CloudQueue CreateQueueClient(string queueName)
        {
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("InfrastructureConnectionString");
            CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();
            CloudQueue queue = queueStorage.GetQueueReference(queueName);
            bool created = queue.CreateIfNotExist();
            if (created)
            {

            }
            return queue;
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IEnumerable<ExamCategory> GetCategorias()
        {
            return this.BCExamQueries.SearchExamCategories();
        }
        [DataObjectMethod(DataObjectMethodType.Insert, false)]
        public void Add(ExamCategory item)
        {            
            item.Id = Guid.NewGuid();
            var dcs = new DataContractSerializer(typeof(BusinessEntities.ExamCategory));
            var mem = new System.IO.MemoryStream();
            dcs.WriteObject(mem, item);
            var queue = CreateQueueClient("skynetlibraryqueue");
            var message = new CloudQueueMessage(mem.ToArray());
            queue.AddMessage(message);                           
        }
        [DataObjectMethod(DataObjectMethodType.Update, false)]
        public void Update(ExamCategory item)
        {
            this.BCExams.MaintenanceExamCategories(new List<ExamCategory>() { item });
        }
        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public void Delete(ExamCategory item)
        {
            this.BCExams.MaintenanceExamCategories(new List<ExamCategory>() { item });
        }
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public ExamCategory GetOne(Guid Id)
        {
            if (Id == Guid.Empty) return null;
            return this.BCExamQueries.SearchExamCategories(SearchExamCategoriesOptions.GetOne , Id).SingleOrDefault();
        }
    }
}
